package Level 
{
	import adobe.utils.CustomActions;
	import adobe.utils.ProductManager;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import Objetos.Chao;
	import Objetos.Heroi;
	import Objetos.Inimigos;
	import Objetos.Obstaculos;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Fase extends MovieClip
	{
		public static var AR_Obstaculos     :Array = new Array();
		public static var AR_Chaos          :Array = new Array();
		
		protected var gameConstructor   :MovieClip;
		protected var UI_pontos         :uint;
		
		protected var MC_chao           :Chao;
		protected var MC_heroi          :Heroi;
		protected var MC_obstaculo      :Obstaculos;
		
	    public static var TX_pontos         :TextField = new TextField();
		protected var TX_pontosFormat   :TextFormat = new TextFormat();
		
		
		
		
		public function Fase() 
		{
		
		}
		
		public function desenharTela():void
		{
			MC_heroi = new Heroi();
			MC_heroi.x = 100;
			MC_heroi.y = 550;
			Main.getInstance().MC_tela.addChild(MC_heroi);
			
			MC_chao = new Chao();
			MC_chao.x = Main.getStage().stageWidth + MC_chao.width / 2;
			MC_chao.y = Main.getStage().stage.stageHeight;
			Main.getInstance().addChild(MC_chao);
			AR_Chaos.push(MC_chao);
			
			MC_chao = new Chao();
			MC_chao.x = Main.getStage().stageWidth/2
			MC_chao.y = Main.getStage().stage.stageHeight;
			Main.getInstance().addChild(MC_chao);
			AR_Chaos.push(MC_chao)

			
			MC_obstaculo = new Obstaculos();
			MC_obstaculo.x = 520
			MC_obstaculo.y = Main.getStage().stage.stageHeight - MC_chao.height / 2 - 30
			Main.getInstance().MC_tela.addChild(MC_obstaculo)
			AR_Obstaculos.push(MC_obstaculo)
			
			/*MC_obstaculo = new Obstaculos();
			MC_obstaculo.x = 800
			MC_obstaculo.y = Main.getStage().stage.stageHeight - MC_chao.height / 2 - 30
			Main.getInstance().addChild(MC_obstaculo)
			AR_Obstaculos.push(MC_obstaculo)
			
			*/
			
		}
		public function update():void
		{	
			Main.getInstance().addChild(TX_pontos);
			
		    TX_pontos.text = "Score:" + UI_pontos.toString();
			TX_pontos.textColor = 0x000066;
			TX_pontos.selectable = false;
			TX_pontos.width = 900;
			
			TX_pontosFormat.align = "left"
	        TX_pontosFormat.font = "Arial";
		    TX_pontosFormat.bold = true
	        TX_pontosFormat.size = 15
		    TX_pontos.defaultTextFormat = TX_pontosFormat;
			
			
			if (MC_heroi != null)
			{
			MC_heroi.update();
			
			for (var i:int = 0; i < AR_Obstaculos.length; i++)
				{
					AR_Obstaculos[i].update();
					if (AR_Obstaculos[i].hitbox.hitTestObject(MC_heroi))
					{
						MC_heroi.y = (AR_Obstaculos[i].y - AR_Obstaculos[i].height / 2) - MC_heroi.height / 2;
						Main.getInstance().BO_morreu = true
						AR_Obstaculos.splice(i, 1)
						
						
						
						//gameOver();
						
						
						
					}
					
					
				}
				
			for ( var k:int = 0; k < AR_Chaos.length; k++)
			{
				AR_Chaos[k].update();
				if ( AR_Chaos[k].hitTestObject(MC_heroi))
				{
					MC_heroi.y = (AR_Chaos[k].y - - MC_chao.height / 2) - MC_heroi.height;
					MC_heroi.NB_velocidadeY = 0;
					MC_heroi.BO_pulando = false
					
				}
				 
			}
			   
			
			if (MC_heroi.BO_pulando == true)
			{
				UI_pontos ++;
				
			}
			
			/*if (Main.getInstance().MC_tela != null)
			{
				if (Main.getInstance().BO_morreu)
				{
					Main.getInstance().removeChild(MC_heroi);
					MC_heroi = null;
					Main.getInstance().removeChild(Main.getInstance().MC_tela)
				}
			}
			*/
			
			
		}
	
	 }
	 
	 
	/* public function gameOver():void
		AR_Chaos.length = 0
	 {
		//removeChild(Main.getInstance().MC_tela)
		//removeEventListener(Event.ENTER_FRAME, update)
		//addChild(Main.getInstance().MC_play)
		
		
		AR_Obstaculos.length = 0
		Main.getInstance().BO_morreu = false
		MC_heroi.BO_pulando = false
		

     }*/
 }
}